/*
 * (C) 2014-2015 Alibaba Group Holding Limited
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *
 *     __  ___   ______        ___       __          ___
 *    |  |/  /  /  __  \      /   \     |  |        /   \
 *    |  '  /  |  |  |  |    /  ^  \    |  |       /  ^  \
 *    |    <   |  |  |  |   /  /_\  \   |  |      /  /_\  \
 *    |  .  \  |  `--'  |  /  _____  \  |  `----./  _____  \
 *    |__|\__\  \______/  /__/     \__\ |_______/__/     \__\
 *
 *
 *
 */
//
// Created by KOALA TEAM on 12/19/14.
//

#import "UIImageView+KLImage.h"


@implementation UIImageView (KLImage)

- (UIImage *)scaleImage:(UIImage *)image {
    CGSize size = self.frame.size;
    CGFloat h = image.size.height;
    CGFloat w = image.size.width;
    if (h <= size.width && w <= size.height) {
        return [self roundedCornerImage:image cornerRadius:0];
    }

    float scale = MAX((float) size.width / w, (float) size.height / h);
    CGSize itemSize = CGSizeMake(MIN(scale * w, w), MIN(scale * h, h));
    if (NULL != UIGraphicsBeginImageContextWithOptions) {
        UIGraphicsBeginImageContextWithOptions(itemSize, NO, 0);
    } else {
        UIGraphicsBeginImageContext(itemSize);
    }

    [image drawInRect:CGRectMake(0, 0, itemSize.width, itemSize.height)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //截图
    return [self croppingWithImage:scaledImage];
}

- (UIImage *)croppingWithImage:(UIImage *)image {
    CGSize desSize = self.frame.size;
    CGSize size = CGSizeMake(desSize.width * [UIScreen mainScreen].scale, desSize.height * [UIScreen mainScreen].scale);
    CGFloat imageWidth = image.size.width * image.scale;
    CGFloat imageHeight = image.size.height * image.scale;
    if (size.width >= imageWidth && size.height >= imageHeight) {
        return [self roundedCornerImage:image cornerRadius:0];
    }

    NSInteger startX = (NSInteger) MAX(0, (imageWidth - size.width) / 2);
    NSInteger startY = (NSInteger) MAX(0, (imageHeight - size.height) / 2);
    CGRect imageRect = CGRectMake(startX, startY, size.width, size.height);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], imageRect);
    UIImage *cropImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return [self roundedCornerImage:cropImage cornerRadius:0];
}

- (UIImage *)roundedCornerImage:(UIImage *)desImage cornerRadius:(CGFloat)radius{
    CGSize desSize = self.frame.size;
    if (radius <= 0) {
        return desImage;
    }
    CGFloat w = desSize.width;
    CGFloat h = desSize.height;
    CGFloat scale = [UIScreen mainScreen].scale;

    if (radius > MIN(w, h)) {
        radius = MIN(w, h) / 2.f;
    }

    UIImage *image = nil;
    CGRect imageFrame = CGRectMake(0.0f, 0.0f, w, h);
    UIGraphicsBeginImageContextWithOptions(desSize, NO, scale);
    [[UIBezierPath bezierPathWithRoundedRect:imageFrame cornerRadius:radius] addClip];
    [desImage drawInRect:imageFrame];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

@end